package com.douwong.student.task;

import android.os.AsyncTask;

import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.WorkResultNotify;
import com.douwong.student.model.ClassFile;
import com.douwong.student.model.ClassNoteInfos;
import com.douwong.student.model.ClassNoteItem;
import com.douwong.student.model.RequestAction;
import com.douwong.student.utils.JsonUtils;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.UIUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * 加载数据
 */
public class ClassFileRecordTask extends AsyncTask<String, Integer, Integer> {

	private WorkResultNotify<ClassNoteItem> clientDone;
	private BaseApplication app;

	private RequestAction action;
	public static int NO_DATA = 0;// 没有新的数据
	public static int REQUESTOK = 1;// 加载更多
	public static int NO_NETWOK = -1;// 没有网络
	private ArrayList<ClassFile>list;
	private String key = null;
	private boolean isSearch=false;
    private List<ClassNoteItem> mItems;

    public ClassFileRecordTask(boolean isSearch, RequestAction action, WorkResultNotify<ClassNoteItem>  clientDone, BaseApplication app){
		this.clientDone=clientDone;
		this.app=app;
		this.action=action;
		this.isSearch=isSearch;
	}
	@SuppressWarnings("unchecked")
	@Override
	/*1为白板2为课堂笔记)*/
	protected Integer doInBackground(String... params) {
		if(params[0].equals("2")){//课堂笔记
			key=BaseApplication.sUserBean.getUserbean().getEuNumber()+"_notes";
		}else if(params[0].equals("1")){//小黑板
			key=BaseApplication.sUserBean.getUserbean().getEuNumber()+"_black";
		}
		if(RequestAction.INIT==action){
			mItems=(ArrayList<ClassNoteItem>) app.readObject(key);
			if(list!=null&&list.size()>0){
				return REQUESTOK;

			}else if(NetUtils.NetwrokIsUseful(UIUtils.getContext())){
				return accessNetworkRequestData(params);
			}else {
				return NO_NETWOK;
			}
		}else {
			if(NetUtils.NetwrokIsUseful(UIUtils.getContext())){
				return accessNetworkRequestData(params);
			}else {
				return NO_NETWOK;
			}
		}
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		if(clientDone!=null){
			clientDone.notify(result, mItems);
		}
	}
	private int accessNetworkRequestData(String... params) {
		String response = null;
		int resultCode =NO_DATA;
		/*subject_id=科目id
class_id=课堂id
file_type=文件类型
pagenum=每页显示数量
*/
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		arrayParams.add(new BasicNameValuePair("class_id", BaseApplication.sUserBean
				.getPersonalbean().getClassId()+""));
		if(params[0]!=null){
			arrayParams.add(new BasicNameValuePair("file_type", params[0]));
		}
		if(params[1]!=null){
			arrayParams.add(new BasicNameValuePair("subject_id", params[1]));
		}else {
			arrayParams.add(new BasicNameValuePair("subject_id", "0"));
		}
		if(params[2]!=null){
			arrayParams.add(new BasicNameValuePair("create_date", params[2]));
		}else {
			arrayParams.add(new BasicNameValuePair("create_date", ""));
		}
			arrayParams.add(new BasicNameValuePair("pagenum", "30"));
		try {
			response = NetUtils
					.httpPost(NetConfig.CLASS_RECORD_FILEINFO+params[3], arrayParams);
			//Log.e("response", response+"");

			if (response!=null){
				ClassNoteInfos classNoteInfos = JsonUtils.parseString(response, ClassNoteInfos.class);
				if (classNoteInfos!=null){
					if (classNoteInfos.isFlag() && classNoteInfos.getData()!=null){

                        mItems = classNoteInfos.getData().getItems();

                        if (mItems !=null && mItems.size()>0){
                            app.saveObject(mItems,key);
                            return REQUESTOK;
                        }


                    }






				}


			}


		} catch (Exception e) {
			e.printStackTrace();
			return NO_DATA;
		}
	return resultCode;
	}
}
